//
//  User+DefaultData.m
//  eyr
//
//  Created by Tor Gjøsæter on 4/24/12.
//  Copyright (c) 2012 University of Bergen. All rights reserved.
//

#import "User+DefaultData.h"
#import "DataPointInTime.h"
#import "Graph+Create.h"
//These categories add methods to the user class, this is convenient if we want to regenerate our user from datamodel, then we have this fucked place to get your shit

#define DEFAULT_USER_NAME @"Testbruker"

@implementation User (DefaultData)

+ (User *)userWithDefaultData:(NSManagedObjectContext *)context //Sending along the DB
{

    User *user = nil;
    
    NSFetchRequest *request  = [NSFetchRequest fetchRequestWithEntityName:@"User"];
    request.predicate = [NSPredicate predicateWithFormat:@"name = %@", DEFAULT_USER_NAME];
    
    NSError *error;
    NSArray *matches = [context executeFetchRequest:request error:&error];
    
    if([matches count] > 1){
        //Bruker finnes allereie
        //Handle error
    }else if ([matches count] == 0)
    {
     //Adding our default user
    user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
    user.name = DEFAULT_USER_NAME;
    [user addGraphsObject:[Graph createRandomGraph:context]];
    [user addGraphsObject:[Graph createRandomGraph:context]];
    [user addGraphsObject:[Graph createRandomGraph:context]];
    }
    
    
    else{
        user = [matches lastObject];
    }
    return user;
}

@end
